package thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo34 {
    public static void main(String[] args) throws InterruptedException {
        // 总共十个子任务提交到线程池中执行
        CountDownLatch latch = new CountDownLatch(10);
        // 容量为10的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for(int i = 0; i < 10; i++){
            int id = i;
            threadPool.submit(()->{
                // 每个循环相隔1秒
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 每个线程打印id并输出
                System.out.println("子任务执行："+ id);
                latch.countDown();
            });
        }
        // 等待所有子任务执行完
        latch.await();
        System.out.println("所有任务执行完毕...");
    }
}
